package com.smrs.goods.service;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.jof.framework.dao.hibernate.BaseDAOHibernateImpl;
import com.jof.framework.util.Pager;
import com.smrs.goods.dao.GoodsCategoryDao;
import com.smrs.goods.model.GoodsAttributeModel;
import com.smrs.goods.model.GoodsAttributeValueModel;
import com.smrs.goods.model.GoodsCategoryModel;
import com.smrs.goods.model.GoodsItemModel;
import com.smrs.service.BaseService;

@Component(value="goodsCategoryService")
public class GoodsCategoryService extends BaseService<GoodsCategoryModel,Long>{
	
	@Autowired
	private GoodsCategoryDao goodsCategoryDao ;
	
	@Autowired
	private GoodsAttributeValueService goodsAttributeValueService;
	
	@Autowired
	private GoodsItemService goodsItemService;
	
	/*
	public Pager<RegionModel> getByNameLikePager(final RegionSearchModel model){
		String name = model.getRegion().getName();
		Pager<RegionModel> pager = goodsCategoryDao.getByNameLikePager(name,model.getPager()); 
		return pager;
		//return null;
	}
	*/
	public Pager<GoodsCategoryModel> getByNameLikePager(String name, Pager<GoodsCategoryModel> pager){
		//String name = model.getRegion().getName();
		Pager<GoodsCategoryModel> tempPager = goodsCategoryDao.getByNameLikePager(name,pager); 
		return tempPager;
		//return null;
	}

	public Pager<GoodsCategoryModel> getFirstLevel(Pager<GoodsCategoryModel> pager){
		//String name = model.getRegion().getName();
		List<GoodsCategoryModel> list = goodsCategoryDao.getSubCategoryByParent(null, 1l);
		Pager<GoodsCategoryModel> tempPager =  pager.cloneFromPager(pager, list.size(), list);
		return tempPager;
		//return null;
	}

	public Pager<GoodsCategoryModel> getSubCategory(Pager<GoodsCategoryModel> pager,Long parentId){
		//String name = model.getRegion().getName();
		List<GoodsCategoryModel> list = goodsCategoryDao.getSubCategoryByParent(parentId, null);
		Pager<GoodsCategoryModel> tempPager =  pager.cloneFromPager(pager, list.size(), list);
		return tempPager;
		//return null;
	}
	
	@Override
	public BaseDAOHibernateImpl<GoodsCategoryModel, Long> getPerformDao() {		
		return goodsCategoryDao;
	}

	public Set<GoodsAttributeModel> getAllContainAttributesByCategoryId(Long id){
		if(id==null){
			Set<GoodsAttributeModel> tempSet = new HashSet<GoodsAttributeModel>();
			return tempSet;			
		}
		GoodsCategoryModel temp = this.getByPK(id);
		Set<GoodsAttributeModel> tempSubSet=new HashSet<GoodsAttributeModel>();
		if(temp.getParent()!=null && temp.getParent().getId()!=null && temp.getParent().getId().longValue()!=0l){
			tempSubSet= getAllContainAttributesByCategoryId(temp.getParent().getId());
		}
		Set<GoodsAttributeModel> tempSet = temp.getAttributes();
		tempSubSet.addAll(tempSet);		
		return tempSubSet;
	}
	
	public List<GoodsCategoryModel> getSubCategoryByParent(Long parentId,Long level){
		List<GoodsCategoryModel> list=goodsCategoryDao.getSubCategoryByParent(parentId,level);
		return list;
	}
	
	public List<GoodsCategoryModel> getChilden(Long parentId){
		List<GoodsCategoryModel> list=goodsCategoryDao.getSubCategoryByParent(parentId,null);
		return list;
	}
	
	public LinkedList<GoodsCategoryModel> getParentsChain(Long modelId){
		 LinkedList<GoodsCategoryModel> list= goodsCategoryDao.getParentsChain(modelId);
		 return list;
	}
	
	public LinkedHashSet<GoodsAttributeModel> getAllAttributeByItemId(Long itemId){
		GoodsItemModel itemModel = goodsItemService.getByPK(itemId);
		return getAllAttributeFromItem(itemModel);
	}
	
	public LinkedHashSet<GoodsAttributeModel> getAllAttributeFromItem(GoodsItemModel itemModel){
		LinkedHashSet<GoodsAttributeModel> goodsAttributeList = new LinkedHashSet<GoodsAttributeModel>();
		Set<GoodsAttributeModel> tempSet;
		if(itemModel.getCategoryId1()!=null){
			tempSet = getAllContainAttributesByCategoryId(itemModel.getCategoryId1());
			if(tempSet!=null){
				goodsAttributeList.addAll(tempSet);
			}
		}
		tempSet = getAllContainAttributesByCategoryId(itemModel.getCategoryId2());
		goodsAttributeList.addAll(tempSet);
		tempSet = getAllContainAttributesByCategoryId(itemModel.getCategoryId3());
		goodsAttributeList.addAll(tempSet);
		tempSet = getAllContainAttributesByCategoryId(itemModel.getCategoryId4());
		goodsAttributeList.addAll(tempSet);
		return goodsAttributeList;
	}
	
	
	public LinkedHashSet<GoodsAttributeModel> getAllAttributeWithValueFromItem(GoodsItemModel itemModel){
		LinkedHashSet<GoodsAttributeModel> goodsAttributeList = getAllAttributeFromItem(itemModel);
		buildValueToAllAttribute(goodsAttributeList);
		return goodsAttributeList;
	}
	
	
	public void buildValueToAllAttribute(HashSet<GoodsAttributeModel> goodsAttributeList){		
		if(goodsAttributeList!=null){
			for(GoodsAttributeModel attribute:goodsAttributeList){
				List<GoodsAttributeValueModel> listValues=goodsAttributeValueService.getGoodsAttributeValueByAttribute(attribute);
				if(listValues!=null){
					attribute.setAttributeValues(listValues);
					for(GoodsAttributeValueModel attributeValue:listValues){
						attributeValue.setGoodsAttribute(attribute);
					}
					
				}
			}
		}		
	}
	
	
	public boolean checkExistItemUseCategory(GoodsCategoryModel goodsCategory){
		List<GoodsItemModel> list = goodsItemService.findByGoodsCategory(goodsCategory);
		if(CollectionUtils.isNotEmpty(list)){
			return true;
		}
		return false;
	}
}
